package com.casa.POI.dianping;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;

public class ListSpider {
	
	private Integer[] cities;
	
	public ListSpider(Integer... cities) {
		this.setCities(cities);
	}
	
	public void setCities(Integer[] cities) {
		this.cities = cities;
	}
	
	public Integer[] getCities() {
		return this.cities;
	}
	
	public void run() {
		TimeUtils.resetGlobalStartTime();
		Console.writeLine("list spider started");
		processCities();
	}

	public void processCities() {
		for (Integer cityId : this.getCities()) {
			Console.writeLine("process city %d...", cityId);
			new SpideCityListExecutor(cityId).execute();
		}
	}

	@SuppressWarnings("unchecked")
	private static Integer[] readCities() {
		Session s = SessionManager.getInstance().openSession();
		Criteria c = s.createCriteria(City.class);
		c.setProjection(Projections.property("id"));
		List<Integer> cities = c.list();
		s.close();
		return cities.toArray(new Integer[0]);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new FetchCitiesExecutor().execute();
		new ListSpider(readCities()).run();
	}

}
